package com.liwenfeng.diancanspringboot.controller.user;

import com.liwenfeng.diancanspringboot.common.result.Result;
import com.liwenfeng.diancanspringboot.pojo.dto.ShoppingCartDTO;
import com.liwenfeng.diancanspringboot.pojo.dto.ShoppingCartSelectAllDto;
import com.liwenfeng.diancanspringboot.pojo.entity.ShoppingCart;
import com.liwenfeng.diancanspringboot.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 **/
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "C端-购物车接口")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车（手机端user模块，首页选菜)
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("购物车数据:{}", shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查看购物车（手机端user模块，首页获取购物车数据)
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> show(Long turnoverId) {
        List<ShoppingCart> list = shoppingCartService.showShoppingCart(turnoverId);
        return Result.success(list);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result clean(Long turnoverId) {
        log.info("清空购物车turnoverId:{}", turnoverId);
        shoppingCartService.cleanShoppingCart(turnoverId);
        return Result.success();
    }

    /**
     * 删除购物车中一个商品（手机端user模块，首页选菜)
     */
    @PostMapping("/sub")
    @ApiOperation("删除购物车中一个商品")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        shoppingCartService.subShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    // 更新购物车,加一，减一，改变单选状态（手机端user模块，购物车下单页面)
    @PutMapping("/update")
    @ApiOperation("更新购物车")
    public Result update(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.updateById(shoppingCart);
        return Result.success();
    }

    // 更改全选状态（手机端user模块)
    @PutMapping("/selectAll")
    @ApiOperation("更新购物车")
    public Result update(@RequestBody ShoppingCartSelectAllDto shoppingCartSelectAllDto) {
        shoppingCartService.updateByIds(shoppingCartSelectAllDto);
        return Result.success();
    }
}
